import pandas as pd

# 加载本地数据
file_path = 'wine/wine.data'    # 使用原始字符串
column_names = ['Class', 'Alcohol', 'Malic_Acid', 'Ash', 'Alcalinity', 'Magnesium', 'Total_Phenols',
                'Flavanoids', 'Nonflavanoid_Phenols', 'Proanthocyanins', 'Color_Intensity', 'Hue',
                'OD280_OD315', 'Proline']
data = pd.read_csv(file_path, names=column_names)

# 查看数据前几行
print(data.head())

# 筛选类别为 1 和 2 的数据
filtered_data = data[data['Class'].isin([1, 2])]

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# 提取特征和标签
X = filtered_data.drop('Class', axis=1)
y = filtered_data['Class']

# 标准化数据
X_scaled = StandardScaler().fit_transform(X)

# PCA 降维
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# 输出降维后的特征
df_pca = pd.DataFrame(data=X_pca, columns=['Principal Component 1', 'Principal Component 2'])
print(df_pca)

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

# LDA 降维
lda = LDA(n_components=1)  # 因为只有两个类别
X_lda = lda.fit_transform(X_scaled, y)

# 输出 LDA 降维后的特征
print(X_lda)